<template>
	<block>
		<view class="image-wrap">
			<view class="image-item relative" :key="index" v-for="(item, index) in photos" @click="$imgPreview(item, index, host, photos)">
				<image class="image" v-if="photos.length" :src="`${host}${item}`" mode="aspectFill"></image>
				<text class="iconfontPT iconshanchutupian delete white" @click.stop="handleDeleteImg(item)"></text>
			</view>
			<view class="chooseImage" @click="handleChooseImage" v-if="photos.length < 3">
				<view class="flex-y-center">
					<text class="iconfontPT icontupian02 block"></text>
					<text class="text">添加图片</text>
				</view>
			</view>
		</view>
		<image-compress style="position: absolute; top: 0rpx; overflow: hidden;" ref="imageCompress" />
	</block>
</template>

<script>
import { identifyYellow } from '../../../apis/public/index';

export default {
	data() {
		return {
			photos: []
		};
	},
	props: {
		uploadType: {
			type: String,
			default: ''
		},
		prop_photos: {
			type: Array,
			default: []
		},
		type: {
			type: String,
			default: 'zj'
		},
		cdn: {
			type: Boolean,
			default: false
		}
	},
	watch: {
		prop_photos() {
			this.photos = this.prop_photos;
			this.inint(this.photos);
		}
	},
	computed: {
		host() {
			return this.cdn?this.$baseHost.qiniuHost:this.$baseHost.car;
		},
		url(){
			return `${this.$baseHost.car}/api/common/${this.cdn ? 'upImage' : 'upload'}`
		},
	},
	methods: {
		inint(photos) {
			this.$emit('onUploadChange', '', photos);
		},
		handleDeleteImg(i) {
			const photos = this.photos;
			this.photos = photos.filter(item => item !== i);
		},
		handleChooseImage() {
			let _this = this;
			if (this.uploadType === 'identify') {
				this.$uploadImage({
					type: 'base64',
					imageCompressFn: this.$refs.imageCompress.start
				}).then(response => {
					_this.$loading.show();
					uni.uploadFile({
						url: _this.url,
						filePath: response.file,
						name: 'file',
						success(res) {
							console.log('11111', res);
							let result = JSON.parse(res.data).data.url;
							_this.photos.push(result);
							_this.$emit('onUploadChange', result, _this.photos);
							_this.$loading.hide();
							identifyYellow({
								type: _this.type,
								base64: response.base64.data
							}).then(res => {
								if (+res.data.result_code === 1) return;

								uni.showToast({
									title: '该图片违规！',
									icon: 'none'
								});
								_this.photos.pop();
							});
						},
						fail(err) {
							console.log(err);
						}
					});
				});
			} else {
				_this.$loading.show();
				this.$uploadImage({
					imageCompressFn: this.$refs.imageCompress.start
				}).then(response => {
					_this.$loading.hide();
					_this.photos.push(response);
					_this.$emit('onUploadChange', response, _this.photos);
				});
			}
		}
	}
};
</script>

<style lang="less">
.image-wrap {
	display: flex;

	.image-item {
		margin-right: 20rpx;

		// &::after {
		// 	position: absolute;
		// 	top: 0;
		// 	left: 0;
		// 	content: '';
		// 	width: 100%;
		// 	height: 100%;
		// 	display: inline-block;
		// 	background: rgba(0, 0, 0, .5);
		// 	z-index: 1;
		// }

		.delete {
			z-index: 2;
			position: absolute;
			right: 5%;
			top: 5%;
		}

		.image {
			width: 220rpx;
			height: 220rpx;
		}
	}

	.chooseImage {
		width: 220rpx;
		height: 220rpx;
		background: #f8f8f8;

		.flex-y-center {
			height: 100%;
		}

		.text {
			font-size: 24rpx;
			color: #999999;
			margin-top: 10rpx;
		}
	}
}
</style>
